<template>
	<div>

		<div class="app-container">
			<TopInfo />

			<Search :lookup="data.lookup" @investigate="inves" />

			<el-button v-prmis="'strategy_payee_add'" type="primary" size="small" @click="add()">
				<template #default>
					<span class="text-sm">{{ $t('strategy.manager.bind') }}</span>
				</template>
			</el-button>

		</div>

		<div class="app-container" v-loading="data.mtloading">
			<el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }"
				:cell-style="{ 'text-align': 'center' }" class="w-full">
				<el-table-column prop="s_id" :label="$t('strategy.manager.strategyID')" />
				<el-table-column prop="manager_id" :label="$t('strategy.manager.accountID')" />
				<el-table-column prop="sort" :label="$t('machine.types.sort')" />
				<el-table-column prop="s_type" :label="$t('strategy.payee.type')">
					<template #default="scope">
						{{ setType(scope.row) }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('public.operate')">
					<template #default="scope">
						<el-button v-prmis="'strategy_payee_del'" type="danger" size="small"
							@click="del(scope.row.sm_id)">{{ $t('public.cancel') }}</el-button>
					</template>
				</el-table-column>

			</el-table>

			<PageNum :total="data.total" :page="data.managerInfo" @exposeData="current" @fraction="fraction" />

		</div>

		<el-dialog v-model="data.dialogVisible" :title="data.title" center width="30%">
			<el-form v-if="data.dialogVisible" :model="data.managerInfo" label-width="100px">
				<span :class="language=='en'?'absolute text-[#ef4444] mt-2 ml-0':'absolute text-[#ef4444] mt-2 ml-7'">*</span>
				<el-form-item :label="$t('strategy.manager.strategyID') + ':'" prop="s_id">
					<el-input v-model="data.managerInfo.s_id" :placeholder="$t('message.pleaseEnter')" />
				</el-form-item>
				<span :class="language=='en'? 'absolute text-[#ef4444] mt-2 ml-0':'absolute text-[#ef4444] mt-2 ml-7'">*</span>
				<el-form-item :label="$t('strategy.manager.accountID') + ':'" prop="manager_id">
					<el-input v-model="data.managerInfo.manager_id" :placeholder="$t('message.pleaseEnter')" />
				</el-form-item>
				<span class="absolute text-[#ef4444] mt-2 ml-11">*</span>
				<el-form-item :label="$t('machine.types.sort') + ':'" prop="sort">
					<el-input v-model.number="data.managerInfo.sort" type="number"
						:placeholder="$t('message.pleaseEnter')" />
				</el-form-item>
				<span :class="language=='en'?'absolute text-[#ef4444] mt-2 ml-0':'absolute text-[#ef4444] mt-2 ml-4'">*</span>
				<el-form-item :label="$t('strategy.strategy.type') + ':'" prop="s_type">
					<el-select v-model="data.managerInfo.s_type" :placeholder="$t('message.pleaseSelect')">
						<el-option :label="$t('strategy.manager.profit')" value="1" />
					</el-select>
				</el-form-item>

				<div class="w-full text-center">
					<div class="inline-block">
						<el-button type="primary" @click="onSave()">{{ $t('public.save') }}</el-button>
						<el-button @click="data.dialogVisible = false">{{ $t('public.cancel') }}</el-button>
					</div>
				</div>

			</el-form>
		</el-dialog>

	</div>

</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import TopInfo from '@/components/TopInfo/index.vue';
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import { managerList, managerBind, managerUnbind } from '@/api/strategy';
import { onMounted } from 'vue'
let language = ref()
const { locale } = useI18n()
onMounted(() => {
	language.value = locale.value
})
let data = reactive({
	mtloading: false, // 加载
	dialogVisible: false, // 弹窗
	title: '', // 弹窗标题
	lookup: [ // 筛选栏
		{ title: $t('strategy.manager.strategyID'), type: 'input', value: '', key: 's_id' },
		{ title: $t('strategy.manager.accountID'), type: 'input', value: '', key: 'manager_id' },
		{ title: $t('strategy.strategy.type'), type: 'define', select: [{ label: $t('strategy.manager.profit'), value: 1 }], value: '', key: 's_type' },
	],
	list: [], // 列表信息
	paytype: [], // 收款类型
	adpayee: {}, // 选中的策略
	managerInfo: { // 筛选数据
		page: 1,
		pageNum: 15
	},
	total: 0, // 总条数
})
let setType = (row: any) => {
	if (row.s_type == 1) {
		row.s_typeCn = $t('strategy.manager.profit')
	}
	return row.s_typeCn
}
// 获取列表信息
let getlist = () => {

	data.mtloading = true
	managerList(data.managerInfo).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			data.list = res.data.data
			data.total = res.data.total
		}
	}).catch(err => {
		data.mtloading = false
	})
}

let onSave = () => {
	if (!data.managerInfo.s_id || !data.managerInfo.manager_id || !data.managerInfo.sort || !data.managerInfo.s_type) {
		ElMessage({
			 message:  $t('message.pleaseFill'),
			type: 'error'
		})
		return
	}
	let obj = {
		s_id: data.managerInfo.s_id,
		manager_id: data.managerInfo.manager_id,
		sort: data.managerInfo.sort,
		s_type: data.managerInfo.s_type
	}
	// 绑定账号
	managerBind(obj).then(res => {
		if (res.state == 200) {
			ElMessage({
				message: $t('message.bindSuccess'),
				type: 'success'
			})
			data.dialogVisible = false
			getlist()
			return
		}
		ElMessage({
			message: $t('message.networkFailed'),
			type: 'error'
		})
	})
}
// 查询回调
let inves = (e) => {
	data.managerInfo.s_id = ''
	data.managerInfo.manager_id = ''
	data.managerInfo.s_type = null
	for (let key in e) {
		if (e[key] != '') {
			data.managerInfo[key] = e[key]
		}
	}
	data.managerInfo.page = 1
	data.managerInfo.pageNum = 15
	getlist()
}

// 页数回调
let current = (e) => {
	data.managerInfo.page = e
	getlist()
}

// 条数回调
let fraction = (e) => {
	data.managerInfo.pageNum = e
	getlist()
}

// 绑定账号
let add = (e, falg) => {
	data.adpayee = {
		s_id: '',
		manager_id: '',
		sort: null,
		s_type: null
	}
	data.title = $t('strategy.manager.bind')
	data.dialogVisible = true
}

let del = (e) => {
	ElMessageBox.confirm($t('message.unbind'))
		.then(res => {
			managerUnbind({ sm_id: e }).then(res => {
				if (res.state == 200) {
					ElMessage({
						message: $t('message.delSuccess'),
						type: 'success'
					})
					data.dialogVisible = false
					getlist()
					return
				}
				ElMessage({
					message: $t('message.networkFailed'),
					type: 'error'
				})
			})
		}).catch(err => {
			console.log('取消')
		})
}

getlist()

</script>
